package com.lb.myeam.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lb.myeam.common.utils.DBUtil;
import com.lb.myeam.core.dao.DepDao;
import com.lb.myeam.core.entity.DepEntity;

public class DepDaoImpl implements DepDao {
	Connection conn = null;
	PreparedStatement pstmt=null;
	ResultSet rs=null;

	public boolean addDep(DepEntity dep) {
		conn = DBUtil.getConn();
		if (null != conn) {
			try {
				String sql="insert into dep values(?,?,?,?)";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, dep.getDepId());
				pstmt.setString(2, dep.getDepAccount());
				pstmt.setString(3, dep.getDepPassword());
				pstmt.setString(4, dep.getDepName());
				
				int num = pstmt.executeUpdate();
				return num > 0;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DBUtil.close(rs, pstmt, conn);
			}
		}
		return false;
	}

	public boolean deleteDep(String id) {
		conn = DBUtil.getConn();
		if (null != conn) {
			try {
				PreparedStatement ps = (PreparedStatement) conn
						.prepareStatement("delete from dep where depid  = ?");
				ps.setString(1, id);
				int num = ps.executeUpdate();
				return num > 0;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DBUtil.close(rs, pstmt, conn);
			}
		}
		return false;
	}

	public List<DepEntity> query(String name) {
		conn = DBUtil.getConn();
		List<DepEntity> list = new ArrayList<DepEntity>();
		if (null != conn) {
			try {
				String sql = "select * from dep";
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				while (rs.next()) {
					list.add(new DepEntity(rs.getString(1), rs.getString(2), rs
							.getString(3),rs.getString(4)));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DBUtil.close(rs, pstmt, conn);
			}
		}
		return list;
	}

	public boolean updateDep(DepEntity dep) {
		conn = DBUtil.getConn();
		if (null != conn) {
			try {
				String sql="update dep set depAccount=?,depPassword=?,depName=? where depId=?";
				pstmt=conn.prepareStatement(sql);
				pstmt.setString(1, dep.getDepAccount());
				pstmt.setString(2, dep.getDepPassword());
				pstmt.setString(3, dep.getDepName());
				pstmt.setString(4, dep.getDepId());
				pstmt.executeUpdate();
				return true;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DBUtil.close(rs, pstmt, conn);
			}
		}
		return false;
	}

	public boolean checkLogin(String depAccount, String depPassword) {
		conn = DBUtil.getConn();
		try {
			pstmt = (PreparedStatement) conn.prepareStatement("select depPassword from dep where depAccount=?");
			pstmt.setString(1, depAccount);
			rs = pstmt.executeQuery();

			if (rs.next()) {
				if (rs.getString("depPassword").equals(depPassword)) {
					return true;
				} else
					return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pstmt, conn);
		}
		return false;
	}
	
	public boolean isExist(String id) {
		try {
			conn = DBUtil.getConn();
			String sql = "select * from dep where depId = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,id);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs, pstmt, conn);
		}
		return false;
	}

}
